package com.ruoyi.web.controller.system;

import com.ruoyi.web.service.C2VersionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*") // 允许所有来源的跨域请求
@RestController
@RequestMapping("/api/c2versioninfo")
public class C2VersionInfoController {

    @Autowired
    private C2VersionInfoService c2VersionInfoService;

    /**
     * 按指定周期获取统计数据
     *
     * @param period 时间周期，可选值："week", "month", "quarter", "year"
     * @return 对应周期的统计数据
     */
    @GetMapping("/period-status")
    public Map<String, Object> getStatsByPeriod(@RequestParam String period) {
        // 验证输入参数
        if (!List.of("week", "month", "quarter", "year").contains(period.toLowerCase())) {
            throw new IllegalArgumentException("Invalid period. Valid values are: week, month, quarter, year.");
        }

        // 打印请求的周期信息
        System.out.println("请求的周期: " + period);

        // 调用服务层方法，根据指定周期获取统计数据
        Map<String, Object> periodData = c2VersionInfoService.getC2VersionInfoByPeriods();

        // 获取所有周期的数据
        @SuppressWarnings("unchecked")
        Map<String, List<Integer>> statuses = (Map<String, List<Integer>>) periodData.get("statuses");
        @SuppressWarnings("unchecked")
        Map<String, List<Integer>> releaseStatuses = (Map<String, List<Integer>>) periodData.get("releaseStatuses");
        @SuppressWarnings("unchecked")
        Map<String, List<Integer>> pubStatuses = (Map<String, List<Integer>>) periodData.get("pubStatuses");
        @SuppressWarnings("unchecked")
        Map<String, List<String>> timePeriods = (Map<String, List<String>>) periodData.get("timePeriods");

        // 创建返回的数据结构
        Map<String, Object> response = new HashMap<>();

        // 根据传入的周期返回对应的时间段数据
        response.put("timePeriods", timePeriods.get(period));

        // 返回当前请求周期的数据
        response.put("statuses", statuses != null ? statuses.get(period) : new ArrayList<>());
        response.put("releaseStatuses", releaseStatuses != null ? releaseStatuses.get(period) : new ArrayList<>());
        response.put("pubStatuses", pubStatuses != null ? pubStatuses.get(period) : new ArrayList<>());

        // 打印返回的数据
        System.out.println("返回的数据: " + response);

        return response;
    }
}